package com.njit.WeStudy.dao;

import com.njit.WeStudy.dataobject.Class;
import java.util.List;

import com.njit.WeStudy.dataobject.ClassView;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface ClassMapper {
    @Delete({
        "delete from class",
        "where class_id = #{classId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer classId);

    @Insert({
        "insert into class (class_name, ",
        "master_id, class_type, ",
        "description, create_time, ",
        "delete_time, is_deleted)",
        "values (#{className,jdbcType=VARCHAR}, ",
        "#{masterId,jdbcType=INTEGER}, #{classType,jdbcType=VARCHAR}, ",
        "#{description,jdbcType=VARCHAR}, now(), ",
        "#{deleteTime,jdbcType=TIMESTAMP}, #{isDeleted,jdbcType=BIT})"
    })
    int insert(Class record);

    @Select({
        "select",
        "class_id, class_name, master_id, class_type, description, create_time, delete_time, ",
        "is_deleted",
        "from class",
        "where class_id = #{classId,jdbcType=INTEGER}"
    })
    @Results(id = "ResultMap",value = {
        @Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="class_name", property="className", jdbcType=JdbcType.VARCHAR),
        @Result(column="master_id", property="masterId", jdbcType=JdbcType.INTEGER),
        @Result(column="class_type", property="classType", jdbcType=JdbcType.VARCHAR),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="delete_time", property="deleteTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="is_deleted", property="isDeleted", jdbcType=JdbcType.BIT)
    })
    Class selectByPrimaryKey(Integer classId);

    //两表级联
    @SelectProvider(type = QueryBuilder.class,method = "selectViewByPrimaryKey")
    @ResultMap("ResultVewMap")
    Class selectViewByPrimaryKey(@Param("classId") int classId);

    @Select({
        "select",
        "class_id, class_name, master_id, class_type, description, create_time, delete_time, ",
        "is_deleted",
        "from class"
    })
    @ResultMap("ResultMap")
    List<Class> selectAll();

    @Select({
            "select",
            "class_id, class_name, master_id, class_type, description, create_time, delete_time, ",
            "is_deleted,user_name,head_image",
            "from class,user",
            "where class.master_id=user.user_id"
    })
    @Results(id = "ResultVewMap",value = {
            @Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="class_name", property="className", jdbcType=JdbcType.VARCHAR),
            @Result(column="master_id", property="masterId", jdbcType=JdbcType.INTEGER),
            @Result(column="class_type", property="classType", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="delete_time", property="deleteTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="is_deleted", property="isDeleted", jdbcType=JdbcType.BIT),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="head_image", property="headImage", jdbcType=JdbcType.VARCHAR)
    })
    List<ClassView> selectAllView();

    @Update({
        "update class",
        "set class_name = #{className,jdbcType=VARCHAR},",
          "master_id = #{masterId,jdbcType=INTEGER},",
          "class_type = #{classType,jdbcType=VARCHAR},",
          "description = #{description,jdbcType=VARCHAR},",
          "delete_time = #{deleteTime,jdbcType=TIMESTAMP},",
          "is_deleted = #{isDeleted,jdbcType=BIT}",
        "where class_id = #{classId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Class record);
}